package control;

import model.object.ObjectUser;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.talknow.R;
import common.Const;

public class Screen3Activity extends Activity implements OnClickListener,
		OnValueChangeListener {
	RadioButton radio1, radio2, radio3;
	ImageView img1, img2, img3;
	EditText text_id;
	TextView text_lat, text_lon;
	NumberPicker picker1, picker2;

	String[] list_sex;
	String[] list_sex_value;
	int sexPosition = 0, agePosition = 18;

	ObjectUser objUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_screen_3);

		picker1 = (NumberPicker) findViewById(R.id.screen_3_picker_sex);
		picker2 = (NumberPicker) findViewById(R.id.screen_3_picker_age);

		text_id = (EditText) findViewById(R.id.screen_3_text_id);
		text_lat = (TextView) findViewById(R.id.screen_3_text_lat);
		text_lon = (TextView) findViewById(R.id.screen_3_text_lon);
		
		text_id.setOnEditorActionListener(new OnEditorActionListener() {

	        @Override
	        public boolean onEditorAction(TextView v, int actionId,
	                KeyEvent event) {
	            if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
	                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

	                in.hideSoftInputFromWindow(text_id
	                        .getApplicationWindowToken(),
	                        InputMethodManager.HIDE_NOT_ALWAYS);
	               return true;

	            }
	            return false;
	        }
	    });

		radio1 = (RadioButton) findViewById(R.id.screen_3_radiobutton_facebook);
		radio2 = (RadioButton) findViewById(R.id.screen_3_radiobutton_line);
		radio3 = (RadioButton) findViewById(R.id.screen_3_radiobutton_skype);

		img1 = (ImageView) findViewById(R.id.screen_3_image_facebook);
		img2 = (ImageView) findViewById(R.id.screen_3_image_line);
		img3 = (ImageView) findViewById(R.id.screen_3_image_skype);

		img1.setOnClickListener(this);
		img2.setOnClickListener(this);
		img3.setOnClickListener(this);

		((Button) findViewById(R.id.screen_3_button_map))
				.setOnClickListener(this);
		((Button) findViewById(R.id.screen_3_button_your_nho))
				.setOnClickListener(this);
		((Button) findViewById(R.id.screen_3_button_your_lon_getid))
				.setOnClickListener(this);

		list_sex = new String[2];
		list_sex[0] = Const.SEX_WOMAN;
		list_sex[1] = Const.SEX_MAN;

		list_sex_value = new String[2];
		list_sex_value[0] = Const.SEX_WOMAN_VALUE;
		list_sex_value[1] = Const.SEX_MAN_VALUE;

		picker1.setMinValue(0);
		picker1.setMaxValue(list_sex.length - 1);
		picker1.setDisplayedValues(list_sex);
		picker1.setOnValueChangedListener(this);

		picker2.setMinValue(18);
		picker2.setMaxValue(60);
		picker2.setOnValueChangedListener(this);

		// AdView adView = new AdView(this, AdSize.BANNER, "a1536b05996f681");
		// RelativeLayout layout = (RelativeLayout)findViewById(R.id.app_admob);
		// layout.addView(adView);
		// AdRequest request = new AdRequest();
		// request.setTesting(false);
		// adView.loadAd(request);
		AdView adView = (AdView) this.findViewById(R.id.app_admob);
		AdRequest request = new AdRequest.Builder()
				.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
				.addTestDevice("9234EBAE83E894E6271DEBE7B380782E").build();
		adView.loadAd(request);

		initData();
	}

	void initData() {
		try {
			objUser = (ObjectUser) getIntent().getExtras().getSerializable(
					Const.KEY_OBJECT);
			text_id.setText(objUser.getUserid());
			text_lat.setText(objUser.getLat());
			text_lon.setText(objUser.getLon());
			for (int i = 0; i < list_sex_value.length; i++) {
				if (list_sex_value[i].equals(objUser.getUsersex())) {
					picker1.setValue(i);
					break;
				}
			}
			picker1.setValue(Integer.valueOf(objUser.getUserage()));
			if (objUser.getUserapp().equals(Const.APP_FACEBOOK)) {
				radio1.setChecked(true);
			} else if (objUser.getUserapp().equals(Const.APP_LINE)) {
				radio2.setChecked(true);
			} else {
				radio3.setChecked(true);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		String userapp = "";
		switch (v.getId()) {
		case R.id.screen_3_image_facebook:
			radio1.setChecked(true);
			break;
		case R.id.screen_3_image_line:
			radio2.setChecked(true);
			break;
		case R.id.screen_3_image_skype:
			radio3.setChecked(true);
			break;
		case R.id.screen_3_button_map:
			intent = new Intent(Screen3Activity.this, Screen4Activity.class);
			startActivityForResult(intent, 1);
			break;
		case R.id.screen_3_button_your_nho:
			if (objUser == null)
				objUser = new ObjectUser();
			objUser.setUserid(text_id.getText().toString());
			objUser.setLat(text_lat.getText().toString());
			objUser.setLon(text_lon.getText().toString());
//			objUser.setLat("35.746494844665094");
//			objUser.setLon("139.72405672073364");
			
			if (radio1.isChecked())
				userapp = Const.APP_FACEBOOK;
			else if (radio2.isChecked())
				userapp = Const.APP_LINE;
			else
				userapp = Const.APP_SKYPE;
			objUser.setUserapp(userapp);
			objUser.setUsersex(list_sex_value[sexPosition]);
			objUser.setUserage(String.valueOf(agePosition));
			Intent data = new Intent();
			Bundle bunder = new Bundle();
			bunder.putSerializable(Const.KEY_OBJECT, objUser);
			data.putExtras(bunder);
			setResult(RESULT_OK, data);
			finish();
			break;
		case R.id.screen_3_button_your_lon_getid:
			if (objUser == null)
				objUser = new ObjectUser();
			objUser.setUserid(text_id.getText().toString());
			objUser.setLat(text_lat.getText().toString());
			objUser.setLon(text_lon.getText().toString());
//			objUser.setLat("35.746494844665094");
//			objUser.setLon("139.72405672073364");
			
			if (radio1.isChecked())
				userapp = Const.APP_FACEBOOK;
			else if (radio2.isChecked())
				userapp = Const.APP_LINE;
			else
				userapp = Const.APP_SKYPE;
			objUser.setUserapp(userapp);
			objUser.setUsersex(list_sex_value[sexPosition]);
			objUser.setUserage(String.valueOf(agePosition));

			if (objUser.getUserid().isEmpty()) {
				Toast toat = Toast.makeText(getBaseContext(), "ID is empty!",
						Toast.LENGTH_SHORT);
				toat.setGravity(Gravity.CENTER, 0, 0);
				toat.show();
				return;
			}
			if (objUser.getLat().isEmpty() || objUser.getLon().isEmpty()) {
				Toast toat = Toast.makeText(getBaseContext(),
						"Lat-Long is empty!", Toast.LENGTH_SHORT);
				toat.setGravity(Gravity.CENTER, 0, 0);
				toat.show();
				return;
			}

			intent = new Intent(Screen3Activity.this, Screen5Activity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable(Const.KEY_OBJECT, objUser);
			intent.putExtras(bundle);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	@Override
	public void onValueChange(NumberPicker arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.screen_3_picker_sex:
			sexPosition = arg2;
			break;
		case R.id.screen_3_picker_age:
			agePosition = arg2;
			break;

		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1 && resultCode == RESULT_OK) {
			Bundle bunder = data.getExtras();
			String lat = bunder.getString(Const.KEY_LAT);
			String lon = bunder.getString(Const.KEY_LON);
			text_lat.setText(lat);
			text_lon.setText(lon);
		}
	}

}
